package com.atjava.webspider.gui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;

import com.atjava.webspider.consts.Const;
import com.atjava.webspider.gui.data.DataIntoGui;
import com.atjava.webspider.gui.help.AboutGui;
import com.atjava.webspider.gui.site.NewSiteGui;
import com.atjava.webspider.gui.site.SiteManageGui;
import com.atjava.webspider.gui.table.DefaultButton;
import com.atjava.webspider.gui.table.DefaultTable;
import com.atjava.webspider.gui.table.DefaultTableModel;
import com.atjava.webspider.gui.task.NewTaskGui;
import com.atjava.webspider.gui.test.TestRegexpGui;
import com.atjava.webspider.gui.tree.JFileTree;
import com.atjava.webspider.gui.tree.JFileTree.FileNode;
import com.atjava.webspider.gui.tree.TaskFileFilter;
import com.atjava.webspider.logic.TaskLogic;
import com.atjava.webspider.model.Task;
import com.atjava.webspider.services.TableService;
import com.atjava.webspider.services.ToolbarService;
import com.atjava.webspider.services.UpdateService;
import com.atjava.webspider.thread.SpiderTask;
import com.atjava.webspider.thread.TaskListener;
import com.atjava.webspider.thread.TaskQueue;
import com.atjava.webspider.util.DialogUtil;
import com.atjava.webspider.util.LogUtil;
import com.atjava.webspider.util.TableUtil;

public class SpiderMainGui extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private JMenu jMenuTask = null;
	private JMenuItem jMenuItemNew = null;
	private JMenuItem jMenuItemOpen = null;
	private JMenuItem jMenuItemSave = null;
	private JMenuItem jMenuItemExit = null;
	private JSplitPane jSplitPaneRight = null;
	private JPanel jPanelMain = null;
	private JSplitPane jSplitPaneMain = null;
	private JScrollPane jPanelLeft = null;
	private JMenu jMenuTool = null;
	private JMenu jMenuHelp = null;
	private JMenuItem jMenuItemOption = null;
	private JMenuItem jMenuItemAbout = null;
	private JFileTree jTree = null;
	private JScrollPane jScrollPaneTaskList = null;
	private JTable jTableTaskList = null;
	private static SpiderMainGui mainFrame;
	private JPanel jPanelToolBar = null;
	private JToolBar jToolBar = null;
	private JButton jButtonStart = null;
	private JButton jButtonPause = null;
	private JButton jButtonStop = null;
	private JButton jButtonDelete = null;
	private DefaultTableModel taskModel = null;
	private JTabbedPane jTabbedPaneInfo = null;
	private JScrollPane jPanelConsole = null;
	private JScrollPane jPanelTaskInfo = null;
	private JTextArea jTextAreaConsole = null;
	private JTextArea jTextAreaTaskInfo = null;
	private JMenu jMenuSite = null;
	private JMenuItem jMenuItemSite = null;
	private JButton jButtonProp = null;
	private JButton jButtonHome = null;
	private TaskLogic taskLogic = null;
	private String currentTaskDir;  //  @jve:decl-index=0:
	private Map<String, List<Task>> taskMap = null;
	private TaskListener taskListener = null;
	private JMenuItem jMenuItemTest = null;
	private TaskQueue queue;
	private JMenuItem jMenuItemUpdate = null;
	private JMenuItem jMenuItemHome = null;
	private JCheckBoxMenuItem jCheckBoxMenuItemShutDown = null;
	private JMenuItem jMenuItemData = null;
	private JMenuItem jMenuItemNewSite = null;
	/**
	 * This is the default constructor
	 */
	private SpiderMainGui() {
		super();
		//window 显示效果
		try {
			//org.fife.plaf.Office2003.Office2003LookAndFeel
			//org.fife.plaf.OfficeXP.OfficeXPLookAndFeel
			//org.fife.plaf.VisualStudio2005.VisualStudio2005LookAndFeel
		    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		    SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception e1) {e1.printStackTrace();}
		LogUtil.info("正在加载任务......");
		this.taskLogic = new TaskLogic();
		try {
			this.taskMap = taskLogic.getAllTask();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//初始化
		LogUtil.info("正在初始化......");
		initialize();
		//启动监听器
		LogUtil.info("正在启动监听器......");
		this.taskListener = new TaskListener(this);
		this.taskListener.start();
		this.taskListener.pause();
		
		//启动线程池
		LogUtil.info("正在启动线程池......");
		this.queue = new TaskQueue();
		this.queue.startup();
	}
	
	public static SpiderMainGui getInstance(){
		if(mainFrame == null){
			mainFrame = new SpiderMainGui();
		}
		return mainFrame;
	}
	
	public String getCurrentTaskDir() {
		return currentTaskDir;
	}

	public void setCurrentTaskDir(String currentTaskDir) {
		this.currentTaskDir = currentTaskDir;
	}
	
	public Map<String, List<Task>> getTaskMap() {
		return taskMap;
	}

	public List<Task> getCurrentTaskList(){
		if(taskMap == null)
			return new ArrayList<Task>();
		Iterator<String> iterator = taskMap.keySet().iterator();
		List<Task> tasks = new ArrayList<Task>();
		while(iterator.hasNext()){
			String path = iterator.next();
			List<Task> currTasks = taskMap.get(path);
			if(path.indexOf(currentTaskDir) >= 0 && currTasks != null){
				tasks.addAll(currTasks);
			}
		}
		return tasks;
	}
	
	public DefaultTableModel getTaskModel(){
		return taskModel;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/spider.png")));
		this.setBounds(new Rectangle(0, 0, 800, 593));
		this.setJMenuBar(getJJMenuBar());
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setContentPane(getJContentPane());
		this.setTitle("WebSpider");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanelMain(), null);
			jContentPane.add(getJPanelToolBar(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenuTask());
			jJMenuBar.add(getJMenuSite());
			jJMenuBar.add(getJMenuTool());
			jJMenuBar.add(getJMenuHelp());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenuTask	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenuTask() {
		if (jMenuTask == null) {
			jMenuTask = new JMenu();
			jMenuTask.setText(" 任务(T) ");
			jMenuTask.setActionCommand("任务");
			jMenuTask.setMnemonic(KeyEvent.VK_T);
			jMenuTask.add(getJMenuItemNew());
			jMenuTask.add(getJMenuItemOpen());
			jMenuTask.add(getJMenuItemSave());
			jMenuTask.addSeparator();
			jMenuTask.add(getJMenuItemExit());
		}
		return jMenuTask;
	}

	/**
	 * This method initializes jMenuItemNew	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemNew() {
		if (jMenuItemNew == null) {
			jMenuItemNew = new JMenuItem();
			jMenuItemNew.setName("MenuItemTask");
			jMenuItemNew.setText("新建任务(N)...");
			jMenuItemNew.setMnemonic(KeyEvent.VK_N);
			jMenuItemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK, false));
			jMenuItemNew.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					NewTaskGui newTaskGui = new NewTaskGui(SpiderMainGui.this,true);
					newTaskGui.setVisible(true);
				}
			});
		}
		return jMenuItemNew;
	}

	/**
	 * This method initializes jMenuItemOpen	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemOpen() {
		if (jMenuItemOpen == null) {
			jMenuItemOpen = new JMenuItem();
			jMenuItemOpen.setText("打开(O)...");
			jMenuItemOpen.setActionCommand("打开(O)...");
			jMenuItemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK, false));
			jMenuItemOpen.setMnemonic(KeyEvent.VK_O);
		}
		return jMenuItemOpen;
	}

	/**
	 * This method initializes jMenuItemSave	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemSave() {
		if (jMenuItemSave == null) {
			jMenuItemSave = new JMenuItem();
			jMenuItemSave.setText("保存(S)");
			jMenuItemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK, false));
			jMenuItemSave.setMnemonic(KeyEvent.VK_S);
		}
		return jMenuItemSave;
	}

	/**
	 * This method initializes jMenuItemExit	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemExit() {
		if (jMenuItemExit == null) {
			jMenuItemExit = new JMenuItem();
			jMenuItemExit.setText("退出(X)");
			jMenuItemExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK, false));
			jMenuItemExit.setMnemonic(KeyEvent.VK_X);
			jMenuItemExit.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return jMenuItemExit;
	}

	/**
	 * This method initializes jSplitPaneRight	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getJSplitPaneRight() {
		if (jSplitPaneRight == null) {
			jSplitPaneRight = new JSplitPane();
			jSplitPaneRight.setDividerSize(3);
			jSplitPaneRight.setDividerLocation(340);
			jSplitPaneRight.setBottomComponent(getJTabbedPaneInfo());
			jSplitPaneRight.setOrientation(JSplitPane.VERTICAL_SPLIT);
			jSplitPaneRight.setTopComponent(getJScrollPaneTaskList());
			jSplitPaneRight.setBorder(BorderFactory.createLineBorder(Color.gray, 0));
		}
		return jSplitPaneRight;
	}

	/**
	 * This method initializes jPanelMain	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelMain() {
		if (jPanelMain == null) {
			jPanelMain = new JPanel();
			jPanelMain.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
			jPanelMain.setLayout(null);
			jPanelMain.setLocation(new Point(1, 45));
			jPanelMain.setSize(new Dimension(792, 494));
			jPanelMain.add(getJSplitPaneMain(), null);
		}
		return jPanelMain;
	}

	/**
	 * This method initializes jSplitPaneMain	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getJSplitPaneMain() {
		if (jSplitPaneMain == null) {
			jSplitPaneMain = new JSplitPane();
			jSplitPaneMain.setDividerSize(3);
			jSplitPaneMain.setBounds(new Rectangle(1, 0, 791, 493));
			jSplitPaneMain.setLeftComponent(getJPanelLeft());
			jSplitPaneMain.setRightComponent(getJSplitPaneRight());
			jSplitPaneMain.setDividerLocation(160);
		}
		return jSplitPaneMain;
	}

	/**
	 * This method initializes jPanelLeft	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JScrollPane getJPanelLeft() {
		if (jPanelLeft == null) {
			jPanelLeft = new JScrollPane();
			//jPanelLeft.setLayout(null);
			//jPanelLeft.add(getJTree(), null);
			jPanelLeft.setBorder(BorderFactory.createLineBorder(Color.gray, 0));
			jPanelLeft.setViewportView(getJTree());
		}
		return jPanelLeft;
	}

	/**
	 * This method initializes jMenuTool	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenuTool() {
		if (jMenuTool == null) {
			jMenuTool = new JMenu();
			jMenuTool.setText("工具(U)");
			jMenuTool.setMnemonic(KeyEvent.VK_U);
			jMenuTool.add(getJMenuItemOption());
			jMenuTool.addSeparator();
			jMenuTool.add(getJMenuItemData());
			jMenuTool.add(getJMenuItemTest());
			jMenuTool.addSeparator();
			jMenuTool.add(getJCheckBoxMenuItemShutDown());
		}
		return jMenuTool;
	}

	/**
	 * This method initializes jMenuHelp	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenuHelp() {
		if (jMenuHelp == null) {
			jMenuHelp = new JMenu();
			jMenuHelp.setText("帮助(H)");
			jMenuHelp.setMnemonic(KeyEvent.VK_H);
			jMenuHelp.add(getJMenuItemUpdate());
			jMenuHelp.add(getJMenuItemHome());
			jMenuHelp.addSeparator();
			jMenuHelp.add(getJMenuItemAbout());
		}
		return jMenuHelp;
	}

	/**
	 * This method initializes jMenuItemOption	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemOption() {
		if (jMenuItemOption == null) {
			jMenuItemOption = new JMenuItem();
			jMenuItemOption.setText("选项(P)...");
		}
		return jMenuItemOption;
	}

	/**
	 * This method initializes jMenuItemAbout	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemAbout() {
		if (jMenuItemAbout == null) {
			jMenuItemAbout = new JMenuItem();
			jMenuItemAbout.setText("关于...");
			jMenuItemAbout.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					SpiderMainGui mainGui = SpiderMainGui.getInstance();
					AboutGui aboutGui = new AboutGui(mainGui,true);
					aboutGui.setVisible(true);
				}
			});
		}
		return jMenuItemAbout;
	}

	/**
	 * This method initializes jTree	
	 * 	
	 * @return javax.swing.JTree	
	 */
	public JFileTree getJTree() {
		if (jTree == null) {
			try {
				File taskHome = new File(Const.TASK_HOME);
				FileNode root = new FileNode(taskHome,new TaskFileFilter());
				jTree = new JFileTree(root,this);
				jTree.setSelectFile(taskHome);
				jTree.expandAll();
			} catch (Exception e) {
				e.printStackTrace();
			}
			jTree.setForeground(new Color(51, 51, 51));
			jTree.setSize(new Dimension(100, 300));
			jTree.setBounds(new Rectangle(1, 2, 100, 300));
			jTree.setLocation(new Point(1, 2));
			jTree.setFont(new Font("Monospaced", Font.PLAIN, 12));
		}
		return jTree;
	}

	/**
	 * This method initializes jScrollPaneTaskList	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneTaskList() {
		if (jScrollPaneTaskList == null) {
			jScrollPaneTaskList = new JScrollPane();
			jScrollPaneTaskList.setViewportView(getJTableTaskList());
			jScrollPaneTaskList.setBorder(BorderFactory.createLineBorder(Color.gray, 0));

		}
		return jScrollPaneTaskList;
	}

	/**
	 * This method initializes jTableTaskList	
	 * 	
	 * @return javax.swing.JTable	
	 */
	public JTable getJTableTaskList() {
		if (jTableTaskList == null) {
			String [] colums = {"编号","状　态","名　称","数 据 量","开始时间","已用时"};
			try {
				TableService tabService = TableService.getInstance();
				List<String []> data = tabService.getTaskTable(getCurrentTaskList());
				taskModel = new DefaultTableModel(colums,data);
				jTableTaskList = new DefaultTable(taskModel);
				TableUtil.setTableAlign(jTableTaskList, JLabel.CENTER);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//自动调整大小
			TableUtil.fitTableColumns(jTableTaskList,new int[]{30,10,200,10,50,50});
			//默认渲染方式
			DefaultTableCellRenderer r = new DefaultTableCellRenderer();   
			r.setHorizontalAlignment(JLabel.CENTER);
			jTableTaskList.setDefaultRenderer(Object.class,r);
			//隐藏第一列
			jTableTaskList.getColumnModel().removeColumn
			(jTableTaskList.getColumnModel().getColumn(0));   
			jTableTaskList.setSize(new Dimension(620, 80));
			jTableTaskList.setRowHeight(25);
			jTableTaskList.setRequestFocusEnabled(false);
			jTableTaskList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			jTableTaskList.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
				public void valueChanged(ListSelectionEvent evt) {
					int n = getSelectRow();
					if(n >= 0){
						ToolbarService.getInstance().setToolBar
						(getCurrentTaskList().get(n).getState());
					}
				}
			});
			jTableTaskList.addMouseListener(new MouseAdapter() {

				public void mouseClicked(MouseEvent e) {
					if(e.getClickCount() == 1)
						return ;
					if(!SpiderMainGui.this.jButtonProp.isEnabled())
						return;
					int n = getSelectRow();
					if(n >= 0){
						Task t = getCurrentTaskList().get(n);
						NewTaskGui tgui = new NewTaskGui(SpiderMainGui.this,true,t);
						tgui.setVisible(true);
					}
				}
				
			});
		}
		return jTableTaskList;
	}

	/**
	 * This method initializes jPanelToolBar	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelToolBar() {
		if (jPanelToolBar == null) {
			jPanelToolBar = new JPanel();
			jPanelToolBar.setLayout(null);
			jPanelToolBar.setBounds(new Rectangle(1, 1, 790, 42));
			jPanelToolBar.add(getJToolBar(), null);
		}
		return jPanelToolBar;
	}

	/**
	 * This method initializes jToolBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private JToolBar getJToolBar() {
		if (jToolBar == null) {
			jToolBar = new JToolBar();
			jToolBar.setLayout(null);
			//jToolBar.setPreferredSize(new Dimension(780, 35));
			//jToolBar.setBorder(BorderFactory.createLineBorder(SystemColor.inactiveCaption, 1));
			jToolBar.setFloatable(false);
			jToolBar.setBounds(new Rectangle(0, 0, 790, 41));
			jToolBar.add(new JLabel("　"));
			jToolBar.add(getJButtonStart());
			jToolBar.add(new JLabel("　"));
			jToolBar.add(getJButtonPause());
			jToolBar.add(new JLabel("　"));
			jToolBar.add(getJButtonStop());
			jToolBar.add(new JLabel("　"));
			jToolBar.add(getJButtonDelete());
			jToolBar.add(new JLabel("　"));
			jToolBar.add(getJButtonProp());
			jToolBar.add(new JLabel("　"));
			jToolBar.add(getJButtonHome());
		}
		return jToolBar;
	}

	/**
	 * This method initializes jButtonStart	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJButtonStart() {
		if (jButtonStart == null) {
			jButtonStart = new DefaultButton();
			jButtonStart.setBounds(new Rectangle(5, 3, 40, 35));
			jButtonStart.setIcon(new ImageIcon(getClass().getResource("/toolbar/start.png")));
			jButtonStart.setFocusPainted(false);
			jButtonStart.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if(!SpiderMainGui.this.jButtonStart.isEnabled())
						return;
					int n = getSelectRow();
					if(n >= 0){
						//继续监听
						SpiderMainGui.this.taskListener.goon();
						try {
							//taskLogic.startTask(getCurrentTaskList().get(n));
							Task t = getCurrentTaskList().get(n);
							if(t.getState() == Const.TASK_STATE_PAUSED){
								queue.goon(t.getId());
							}else{
								SpiderTask st = new SpiderTask(t);
								queue.execute(st);
							}
							SpiderMainGui.this.jButtonStart.setEnabled(false);
						} catch (Exception e1) {
							e1.printStackTrace();
							DialogUtil.showError(SpiderMainGui.this, "启动任务失败！");
						}
					}
				}
			});
		}
		return jButtonStart;
	}

	/**
	 * This method initializes jButtonPause	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJButtonPause() {
		if (jButtonPause == null) {
			jButtonPause = new DefaultButton();
			jButtonPause.setBounds(new Rectangle(60, 3, 40, 35));
			jButtonPause.setFocusPainted(false);
			jButtonPause.setIcon(new ImageIcon(getClass().getResource("/toolbar/pause.png")));
			jButtonPause.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if(!SpiderMainGui.this.jButtonPause.isEnabled())
						return;
					int n = getSelectRow();
					if(n >= 0){
						//暂停监听
						SpiderMainGui.this.taskListener.pause();
						Task t = getCurrentTaskList().get(n);
						queue.pause(t.getId());
					}
				}
			});
		}
		return jButtonPause;
	}

	/**
	 * This method initializes jButtonStop	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJButtonStop() {
		if (jButtonStop == null) {
			jButtonStop = new DefaultButton();
			jButtonStop.setBounds(new Rectangle(115, 3, 40, 35));
			jButtonStop.setFocusPainted(false);
			jButtonStop.setIcon(new ImageIcon(getClass().getResource("/toolbar/stop.png")));
			jButtonStop.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if(!SpiderMainGui.this.jButtonStop.isEnabled())
						return;
					int n = getSelectRow();
					if(n >= 0){
						Task t = getCurrentTaskList().get(n);
						queue.stop(t.getId());
					}
				}
			});
		}
		return jButtonStop;
	}
	
	private int getSelectRow(){
		//行数和选中行
		int rows = taskModel.getRowCount();
		int n = jTableTaskList.getSelectedRow();
		if(rows > 0 && n >= 0)
			return n;
		return -1;
	}

	/**
	 * This method initializes jButtonDelete	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJButtonDelete() {
		if (jButtonDelete == null) {
			jButtonDelete = new DefaultButton();
			jButtonDelete.setBounds(new Rectangle(170, 3, 40, 35));
			jButtonDelete.setFocusPainted(false);
			jButtonDelete.setIcon(new ImageIcon(getClass().getResource("/toolbar/del.png")));
			jButtonDelete.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if(!SpiderMainGui.this.jButtonDelete.isEnabled())
						return;
					int n = getSelectRow();
					if(n >= 0){
						Task t = getCurrentTaskList().get(n);
						if(DialogUtil.confirmMessage(SpiderMainGui.this, "真的要删除 [ " + t.getName() + " ] 这个任务吗？")){
							if(getCurrentTaskList().remove(t)){
								File tFile = new File(t.getId());
								tFile.delete();
								taskModel.removeData(n);
							}
						}
					}
				}
			});
		}
		return jButtonDelete;
	}

	/**
	 * This method initializes jTabbedPaneInfo	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPaneInfo() {
		if (jTabbedPaneInfo == null) {
			jTabbedPaneInfo = new JTabbedPane();
			jTabbedPaneInfo.setBounds(new Rectangle(0, 0, 623, 150));
			jTabbedPaneInfo.setTabPlacement(JTabbedPane.BOTTOM);
			jTabbedPaneInfo.addTab("控制台", null, getJPanelConsole(), null);
			jTabbedPaneInfo.addTab("任　务", null, getJPanelTaskInfo(), null);
			jTabbedPaneInfo.setBorder(BorderFactory.createLineBorder(Color.gray, 0));
		}
		return jTabbedPaneInfo;
	}

	/**
	 * This method initializes jPanelConsole	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJPanelConsole() {
		if (jPanelConsole == null) {
			jPanelConsole = new JScrollPane();
			//jPanelConsole.setLayout(null);
			//jPanelConsole.add(getJTextAreaConsole(), null);
			jPanelConsole.setBorder(BorderFactory.createLineBorder(Color.gray, 0));
			jPanelConsole.setViewportView(getJTextAreaConsole());
		}
		return jPanelConsole;
	}

	/**
	 * This method initializes jPanelTaskInfo	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJPanelTaskInfo() {
		if (jPanelTaskInfo == null) {
			jPanelTaskInfo = new JScrollPane();
			//jPanelTaskInfo.setLayout(null);
			//jPanelTaskInfo.add(getJTextAreaTaskInfo(), null);
			jPanelTaskInfo.setViewportView(getJTextAreaTaskInfo());
			jPanelTaskInfo.setBorder(BorderFactory.createLineBorder(Color.gray, 0));
		}
		return jPanelTaskInfo;
	}

	/**
	 * This method initializes jTextAreaConsole	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	public JTextArea getJTextAreaConsole() {
		if (jTextAreaConsole == null) {
			jTextAreaConsole = new JTextArea();
			jTextAreaConsole.setBounds(new Rectangle(2, 2, 615, 116));
			jTextAreaConsole.setEditable(false);
			jTextAreaConsole.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 13));
			jTextAreaConsole.setForeground(SystemColor.desktop);
			jTextAreaConsole.setEnabled(true);
		}
		return jTextAreaConsole;
	}

	/**
	 * This method initializes jTextAreaTaskInfo	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	public JTextArea getJTextAreaTaskInfo() {
		if (jTextAreaTaskInfo == null) {
			jTextAreaTaskInfo = new JTextArea();
			jTextAreaTaskInfo.setBounds(new Rectangle(2, 2, 615, 116));
			jTextAreaTaskInfo.setEditable(false);
			jTextAreaTaskInfo.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			jTextAreaTaskInfo.setEnabled(true);
		}
		return jTextAreaTaskInfo;
	}

	/**
	 * This method initializes jMenuSite	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenuSite() {
		if (jMenuSite == null) {
			jMenuSite = new JMenu();
			jMenuSite.setText("站点(S)");
			jMenuSite.setActionCommand("站点");
			jMenuSite.setMnemonic(KeyEvent.VK_S);
			jMenuSite.add(getJMenuItemNewSite());
			jMenuSite.addSeparator();
			jMenuSite.add(getJMenuItemSite());
		}
		return jMenuSite;
	}

	/**
	 * This method initializes jMenuItemSite	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemSite() {
		if (jMenuItemSite == null) {
			jMenuItemSite = new JMenuItem();
			jMenuItemSite.setText("站点管理...");
			jMenuItemSite.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					SiteManageGui siteGui = new SiteManageGui(SpiderMainGui.this,true);
					siteGui.setVisible(true);
				}
			});
		}
		return jMenuItemSite;
	}

	/**
	 * This method initializes jButtonProp	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJButtonProp() {
		if (jButtonProp == null) {
			jButtonProp = new DefaultButton();
			jButtonProp.setFocusPainted(false);
			jButtonProp.setIcon(new ImageIcon(getClass().getResource("/toolbar/prop2.png")));
			jButtonProp.setBounds(new Rectangle(225, 3, 40, 35));
			jButtonProp.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if(!SpiderMainGui.this.jButtonProp.isEnabled())
						return;
					int n = getSelectRow();
					if(n >= 0){
						Task t = getCurrentTaskList().get(n);
						NewTaskGui tgui = new NewTaskGui(SpiderMainGui.this,true,t);
						tgui.setVisible(true);
					}
				}
			});
		}
		return jButtonProp;
	}

	/**
	 * This method initializes jButtonHome	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonHome() {
		if (jButtonHome == null) {
			jButtonHome = new DefaultButton();
			jButtonHome.setBounds(new Rectangle(280, 3, 40, 35));
			jButtonHome.setFocusPainted(false);
			jButtonHome.setIcon(new ImageIcon(getClass().getResource("/toolbar/about.png")));
			jButtonHome.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					UpdateService.getInstance().visitHome();
				}
			});
		}
		return jButtonHome;
	}

	/**
	 * This method initializes jMenuItemTest	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemTest() {
		if (jMenuItemTest == null) {
			jMenuItemTest = new JMenuItem();
			jMenuItemTest.setText("正则表达式测试...");
			jMenuItemTest.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					TestRegexpGui newTaskGui = TestRegexpGui.getInstance();
					newTaskGui.setVisible(true);
				}
			});
		}
		return jMenuItemTest;
	}

	/**
	 * This method initializes jMenuItemUpdate	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemUpdate() {
		if (jMenuItemUpdate == null) {
			jMenuItemUpdate = new JMenuItem();
			jMenuItemUpdate.setText("检查更新");
			jMenuItemUpdate.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					
				}
			});
		}
		return jMenuItemUpdate;
	}

	/**
	 * This method initializes jMenuItemHome	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemHome() {
		if (jMenuItemHome == null) {
			jMenuItemHome = new JMenuItem();
			jMenuItemHome.setText("访问主页");
			jMenuItemHome.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					UpdateService.getInstance().visitHome();
				}
			});
		}
		return jMenuItemHome;
	}

	/**
	 * This method initializes jCheckBoxMenuItemShutDown	
	 * 	
	 * @return javax.swing.JCheckBoxMenuItem	
	 */
	private JCheckBoxMenuItem getJCheckBoxMenuItemShutDown() {
		if (jCheckBoxMenuItemShutDown == null) {
			jCheckBoxMenuItemShutDown = new JCheckBoxMenuItem();
			jCheckBoxMenuItemShutDown.setText("完成后关机(D)");
		}
		return jCheckBoxMenuItemShutDown;
	}

	/**
	 * This method initializes jMenuItemData	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemData() {
		if (jMenuItemData == null) {
			jMenuItemData = new JMenuItem();
			jMenuItemData.setName("MenuItemData");
			jMenuItemData.setText("数据导入...");
			jMenuItemData.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					DataIntoGui intoGui = new DataIntoGui(SpiderMainGui.this,true);
					intoGui.setVisible(true);
				}
			});
			
		}
		return jMenuItemData;
	}
	
	public TaskListener getTaskListener(){
		return taskListener;
	}

	/**
	 * This method initializes jMenuItemNewSite	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemNewSite() {
		if (jMenuItemNewSite == null) {
			jMenuItemNewSite = new JMenuItem();
			jMenuItemNewSite.setName("MenuItemNewSite");
			jMenuItemNewSite.setText("新建站点...");
			jMenuItemNewSite.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					NewSiteGui newSiteGui = new NewSiteGui(SpiderMainGui.this,true);
					newSiteGui.setVisible(true);
				}
			});
			
		}
		return jMenuItemNewSite;
	}

}  //  @jve:decl-index=0:visual-constraint="-17,31"
